package com.icongyou.enterprise.talent_analysis.mapper;

import com.icongyou.enterprise.talent_analysis.entity.WordCloudCache;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 词云图缓存Mapper测试
 */
@SpringBootTest
@Transactional
public class WordCloudCacheMapperTest {

    @Autowired
    private WordCloudCacheMapper mapper;

    // 使用数据库中存在的学生ID
    private static final String STUDENT_1 = "660e8400-e29b-41d4-a716-446655440101"; // 张三
    private static final String STUDENT_2 = "660e8400-e29b-41d4-a716-446655440102"; // 李四
    private static final String STUDENT_3 = "660e8400-e29b-41d4-a716-446655440103"; // 王五

    @BeforeEach
    public void setUp() {
        // 清理测试学生的词云缓存数据,确保测试独立性
        mapper.deleteByStudentId(STUDENT_1);
        mapper.deleteByStudentId(STUDENT_2);
        mapper.deleteByStudentId(STUDENT_3);
    }

    @Test
    public void testInsertAndSelect() {
        WordCloudCache cache = createCache(STUDENT_1);
        
        int result = mapper.insert(cache);
        assertEquals(1, result);

        WordCloudCache retrieved = mapper.selectByStudentId(STUDENT_1);
        assertNotNull(retrieved);
        assertEquals(STUDENT_1, retrieved.getStudentId());
        assertNotNull(retrieved.getKeywordsJson());
    }

    @Test
    public void testSelectByStudentId_notFound() {
        WordCloudCache retrieved = mapper.selectByStudentId("nonexistent-student");
        assertNull(retrieved);
    }

    @Test
    public void testUpdate() {
        WordCloudCache cache = createCache(STUDENT_2);
        cache.setVersion(1);
        mapper.insert(cache);

        // 查询出来以获取插入后的真实version
        WordCloudCache inserted = mapper.selectByStudentId(STUDENT_2);
        assertNotNull(inserted);
        
        // 更新 (version从insert时的0开始)
        inserted.setKeywordsJson("{\"updated\":10,\"data\":5}");
        inserted.setUpdatedAt(LocalDateTime.now());
        
        int result = mapper.update(inserted);
        assertEquals(1, result);

        WordCloudCache updated = mapper.selectByStudentId(STUDENT_2);
        assertNotNull(updated);
        assertTrue(updated.getKeywordsJson().contains("updated"));
        assertEquals(1, updated.getVersion()); // version从0变成1
    }

    @Test
    public void testUpdate_notExists() {
        WordCloudCache cache = createCache("nonexistent-student");
        
        int result = mapper.update(cache);
        assertEquals(0, result);
    }

    @Test
    public void testDeleteByStudentId() {
        WordCloudCache cache = createCache(STUDENT_3);
        mapper.insert(cache);

        int result = mapper.deleteByStudentId(STUDENT_3);
        assertEquals(1, result);

        // 验证已删除
        WordCloudCache retrieved = mapper.selectByStudentId(STUDENT_3);
        assertNull(retrieved);
    }

    @Test
    public void testDeleteByStudentId_notExists() {
        int result = mapper.deleteByStudentId("nonexistent-student");
        assertEquals(0, result);
    }

    @Test
    public void testInsert_withComplexKeywordsJson() {
        WordCloudCache cache = createCache(STUDENT_1);
        String complexJson = "{\"Java\":100,\"Spring\":80,\"MySQL\":60}";
        cache.setKeywordsJson(complexJson);
        
        mapper.insert(cache);

        WordCloudCache retrieved = mapper.selectByStudentId(STUDENT_1);
        assertNotNull(retrieved);
        assertNotNull(retrieved.getKeywordsJson());
        assertTrue(retrieved.getKeywordsJson().contains("Java"));
    }

    @Test
    public void testUpdate_withAiUsed() {
        WordCloudCache cache = createCache(STUDENT_2);
        cache.setAiUsed(false);
        mapper.insert(cache);

        // 查询出来获取插入后的状态
        WordCloudCache inserted = mapper.selectByStudentId(STUDENT_2);
        assertNotNull(inserted);
        
        // 更新使用AI
        inserted.setAiUsed(true);
        inserted.setKeywordsJson("{\"AI\":100}");
        mapper.update(inserted);

        WordCloudCache retrieved = mapper.selectByStudentId(STUDENT_2);
        assertNotNull(retrieved);
        assertTrue(retrieved.getAiUsed());
        assertEquals(1, retrieved.getVersion()); // version从0变成1
    }

    @Test
    public void testUpdate_incrementVersion() {
        WordCloudCache cache = createCache(STUDENT_3);
        mapper.insert(cache);

        // 第一次更新
        WordCloudCache first = mapper.selectByStudentId(STUDENT_3);
        first.setKeywordsJson("{\"v\":2}");
        mapper.update(first);

        // 第二次更新
        WordCloudCache second = mapper.selectByStudentId(STUDENT_3);
        second.setKeywordsJson("{\"v\":3}");
        mapper.update(second);

        WordCloudCache retrieved = mapper.selectByStudentId(STUDENT_3);
        assertNotNull(retrieved);
        assertEquals(2, retrieved.getVersion()); // 0->1->2
    }

    // ==================== Helper Methods ====================

    private WordCloudCache createCache(String studentId) {
        WordCloudCache cache = new WordCloudCache();
        cache.setStudentId(studentId);
        cache.setKeywordsJson("{\"test\":1}");
        cache.setAiUsed(false);
        cache.setGeneratedAt(LocalDateTime.now());
        cache.setUpdatedAt(LocalDateTime.now());
        cache.setVersion(1);
        return cache;
    }
}

